package hibernatemock;

import javax.persistence.EntityManagerFactory;

import hibernatemock.persistence.PersistenceUtil;

@SuppressWarnings("unchecked") public class HibernateMock {

	private FluentApi fluentApi = Defaults.get();
	private PersistenceUtil persistenceUtil;

	public HibernateMock(EntityManagerFactory entityManagerFactory) {
		persistenceUtil = new PersistenceUtil(entityManagerFactory);
	}

	public HibernateMock when(Class<?> any) {
		fluentApi.putKey(any);
		return this;
	}

	public HibernateMock deliver(Object... any) {
		if (any.length == 1) {
			fluentApi.putValue(any[0]);
		} else {
			for (int i = 0; i < any.length; i = i + 2)
				fluentApi.setValue((String) any[i], any[i + 1]);
		}
		return this;
	}

	public <T> T any(Class<T> any) {
		Object obj = fluentApi.getValue(new SmartClass(any));
		fluentApi.clear();
		persistenceUtil.persist(obj);
		return (T) obj;
	}

}
